package com.qf.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.qf.mapper.UserMapper;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 演示转账
     */
    @Override
    @Transactional
    @LcnTransaction
    public void transferMoney() {

        //远程调用serviceB服务, 账户2加500元, 调用服务b的controller接口
        restTemplate.getForObject("http://localhost:8082/transfer", String.class);

        //创造异常, 让事务回滚, 试验微服务架构下分布式事务是否好用
//        int i = 1 / 0;

        //本地调用, 账户1扣款500元
        userMapper.update();
    }
}